package jtacck.labmedicine;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;

/**
 * Handles the screen that shows one package's data.
 * 
 * @author Kevin
 * 
 */
public class PackageActivity extends Activity {
    private String[] data;
    private Bundle extras;
    private OnClickListener listener;
    private String barcode;
    private CheckBox check;
    private boolean sent;


    /**
     * Called when PackageActivity is started. Uses a bundle labeled "jtacck"
     * supplied by the creating Activity to display the proper information. This bundle
     * is an extra of the Intent for the PackageActivity (NOT savedInstanceState).
     * Bundle jtacck must include: 
     *      A StringArray with key "contents" that represents the contents of the package. 
     *      An int with key "selectedID" that is the requestCode when this 
     *          activity is started for result.
     *      A String with key "title" that is the text to be displayed at the top of the screen.
     *      A String with key "barcode" that is the text representation of the package's 
     *          unique ID.
     *      A String with key "requirements" that represents the requirements of the package.
     * 
     * @param savedInstanceState
     * @Override
     */
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        extras = getIntent().getBundleExtra("jtacck");
        setContentView(R.layout.package_screen);
        
        
        TextView title = (TextView) findViewById(R.id.package_screen_title);
        TextView barcodeText = (TextView) findViewById(R.id.package_barcode);
        TextView requirements = (TextView) findViewById(R.id.package_requirements);
        ListView contents = (ListView) findViewById(R.id.package_contents);
        check = (CheckBox) findViewById(R.id.package_checkbox);
        
        data = extras.getStringArray("contents");
        contents.setAdapter(new ArrayAdapter<String>(this,
                R.layout.listview_text_format, data));
        
        barcode = extras.getString("barcode");
        title.setText(extras.getString("title"));
        barcodeText.setText("Barcode: " + barcode);
        requirements.setText(extras.getString("requirements"));
        if(extras.getBoolean("collected")){
            check.setChecked(true);
            sent = true;
            setResult(1);
        }else {
            check.setChecked(false);
            sent = false;
            setResult(-extras.getInt("selectedID"));
        }
        
        

        listener = new OnClickListener() {
            public void onClick(View view) {
                if(!sent) {
                    MainActivity.linker.packageCollected(barcode);
                    sent = true;
                    MainActivity.sList = MainActivity.linker
                    .getShippingList(MainActivity.currentSite);
                    MainActivity.cache.collect(barcode);
                }
                check.setChecked(true);
                setResult(1);
            }
        };
        
        check.setOnClickListener(listener);
    }
    

}